package com.ruoyi.gd.xcaplog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gd.xcaplog.domain.bo.ZXcapLogBo;
import com.ruoyi.gd.xcaplog.domain.vo.ZXcapLogVo;
import com.ruoyi.gd.xcaplog.domain.ZXcapLog;
import com.ruoyi.gd.xcaplog.mapper.ZXcapLogMapper;
import com.ruoyi.gd.xcaplog.service.IZXcapLogService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 下场安排日志Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-31
 */
@RequiredArgsConstructor
@Service
public class ZXcapLogServiceImpl implements IZXcapLogService {

    private final ZXcapLogMapper baseMapper;

    /**
     * 查询下场安排日志
     */
    @Override
    public ZXcapLogVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询下场安排日志列表
     */
    @Override
    public TableDataInfo<ZXcapLogVo> queryPageList(ZXcapLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZXcapLog> lqw = buildQueryWrapper(bo);
        Page<ZXcapLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询下场安排日志列表
     */
    @Override
    public List<ZXcapLogVo> queryList(ZXcapLogBo bo) {
        LambdaQueryWrapper<ZXcapLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZXcapLog> buildQueryWrapper(ZXcapLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZXcapLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getXcid()), ZXcapLog::getXcid, bo.getXcid());
        lqw.like(StringUtils.isNotBlank(bo.getXcfzrname()), ZXcapLog::getXcfzrname, bo.getXcfzrname());
        lqw.like(StringUtils.isNotBlank(bo.getXcrname()), ZXcapLog::getXcrname, bo.getXcrname());
        lqw.eq(StringUtils.isNotBlank(bo.getXclx()), ZXcapLog::getXclx, bo.getXclx());
        lqw.eq(StringUtils.isNotBlank(bo.getXcbz()), ZXcapLog::getXcbz, bo.getXcbz());
        lqw.eq(bo.getXcsj() != null, ZXcapLog::getXcsj, bo.getXcsj());
        lqw.eq(bo.getCreatetime() != null, ZXcapLog::getCreatetime, bo.getCreatetime());
          lqw.orderByDesc(ZXcapLog::getCreatetime);
        return lqw;
    }

    /**
     * 新增下场安排日志
     */
    @Override
    public Boolean insertByBo(ZXcapLogBo bo) {
        ZXcapLog add = BeanUtil.toBean(bo, ZXcapLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改下场安排日志
     */
    @Override
    public Boolean updateByBo(ZXcapLogBo bo) {
        ZXcapLog update = BeanUtil.toBean(bo, ZXcapLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZXcapLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除下场安排日志
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<ZXcapLogVo> queryListByPId(String ids) {
        List<ZXcapLogVo> listXcLog = baseMapper.queryListByPId(ids);
        return listXcLog;
    }
}
